import { Component, OnInit } from '@angular/core';
import { HttpClient} from '@angular/common/http';
import {StorageService} from '../services/storage.service';
// import any = jasmine.any;
// import {any} from 'codelyzer/util/function';
@Component({
  selector: 'app-shopping-cart',
  templateUrl: './shopping-cart.component.html',
  styleUrls: ['./shopping-cart.component.scss']
})
export class ShoppingCartComponent implements OnInit {
  public title: any = '购物车';

  // 接收的商品列表
  public goods: any = [];
  // 全选控制
  public all = true;
  // 控制提示窗显示
  public ProShow = false;
  // 选中商品
  public ongoods: any[] = [];
  // 总价
  public allnum = 0;
  // 临时计算
  public full: any;
  // 选中物品总数
  public allcount: any;
  constructor(public http: HttpClient, public storage: StorageService) { }
  // 获取购物车列表
  ngOnInit() {
    const api = 'http://192.168.2.227:8080/shoppingcart/personal?user_id=' + this.storage.get('id');
    console.log(api)
    this.http.get(api).subscribe((response: any) => {
      console.log(response);
      this.goods = response.data;
      console.log(this.goods);
    });
  }
  // 删除商品
  delete(i) {
    this.ProShow = true;
  }
  // 按钮显示
  on(i) {
    console.log(this.goods[i]);
    // 按钮显示
    this.goods[i].status = !this.goods[i].status;
    this.all = true;

    // 如果按钮显示
    if (this.goods[i].status) {
      this.ongoods.push(this.goods[i].g_price);
      this.allnum = this.getMoney(this.ongoods);
      console.log(this.allnum);
    } else {  // 如果按钮不显示
           if (this.ongoods.length === 1) {
            this.ongoods = [];
            }
            this.ongoods.splice(i, 1);
            this.allnum = this.getMoney(this.ongoods);
    }
  }
  // 全选
  allOn() {

    this.all = !this.all;

    if (this.all) {
      for (let i = 0 ; i < this.goods.length; i++) {
        this.goods[i].status = false;
      }
    } else {
      for (let i = 0 ; i < this.goods.length; i++) {
        this.goods[i].status = true;
      }
    }
  }

  // 提示框
  cancel() {
    this.ProShow = false;
  }

  alldet() {
    console.log(this.ongoods);
    // for (let a = 0; a < this.ongoods.length ; a++ ) {
    //     this.allnum = this.allnum + this.ongoods[a].g_price;
    //   }
  }


  getMoney(arr) {
    this.full = 0;
    for (let a = 0; a < arr.length ; a++ ) {
      this.full = this.full + arr[a];
    }
    return this.full;
  }
}
